package com.net.wanjian.networkhospitalmanager.activity.my;

import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.activity.LoginActivity;
import com.net.wanjian.networkhospitalmanager.activity.MainActivity;
import com.net.wanjian.networkhospitalmanager.activity.base.BaseActivity;
import com.net.wanjian.networkhospitalmanager.consts.SharedPreferencesKeyConst;
import com.net.wanjian.networkhospitalmanager.netstatus.NetUtils;
import com.net.wanjian.networkhospitalmanager.utils.DataCleanManager;
import com.net.wanjian.networkhospitalmanager.utils.ProgressDialogUtils;
import com.net.wanjian.networkhospitalmanager.utils.SharedXmlUtil;

import java.util.LinkedHashSet;
import java.util.Set;

import butterknife.BindView;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * Description:
 * Created by lidongcai on 17/5/2.
 * Email:  lidongcaiocc@163.com
 */
public class SettingActivity extends BaseActivity implements View.OnClickListener {


    @BindView(R.id.top_back_layout)
    LinearLayout topBackLayout;
    @BindView(R.id.top_title_tv)
    TextView topTitleTv;
    @BindView(R.id.setting_switch_user_layout)
    RelativeLayout settingSwitchUserLayout;
    @BindView(R.id.setting_switch_role_layout)
    RelativeLayout settingSwitchRoleLayout;
    @BindView(R.id.setting_about_we_layout)
    RelativeLayout settingAboutWeLayout;
    @BindView(R.id.setting_clear_cache_layout)
    RelativeLayout settingClearCacheLayout;
    @BindView(R.id.setting_cancel_btn)
    Button settingCancelBtn;
    @BindView(R.id.setting_clear_cache_tv)
    TextView settingClearCacheTv;
    @BindView(R.id.setting_edit_password_layout)
    RelativeLayout settingEditPasswordLayout;

    @Override
    protected void initData() {
        topTitleTv.setText("设置");
        topBackLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingActivity.this.finish();
            }
        });

        settingSwitchUserLayout.setOnClickListener(this);
        settingSwitchRoleLayout.setOnClickListener(this);
        settingAboutWeLayout.setOnClickListener(this);
        settingClearCacheLayout.setOnClickListener(this);
        settingEditPasswordLayout.setOnClickListener(this);
        settingCancelBtn.setOnClickListener(this);

        //设置当前缓存占用空间
        try {
            settingClearCacheTv.setText(DataCleanManager.getTotalCacheSize(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setting_edit_password_layout://修改密码
                openActivity(EditPasswordActivity.class);
                break;
            case R.id.setting_switch_user_layout:
                break;
            case R.id.setting_switch_role_layout:
                openActivity(SwitchRoleActivity.class);
                break;
            case R.id.setting_about_we_layout://关于我们
                openActivity(AboutUsActivity.class);
                break;
            case R.id.setting_clear_cache_layout://清除缓存
                DataCleanManager.clearAllCache(this);
                try {
                    settingClearCacheTv.setText(DataCleanManager.getTotalCacheSize(this));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.setting_cancel_btn://用户注销
                ProgressDialogUtils.showAskDialog(this, "", "确定注销当前用户吗？", "确认", "取消", new ProgressDialogUtils.OnClickDialogBtnListenner() {
                    @Override
                    public void onClickDialogBtn(int btnId) {
                        switch (btnId){
                            case 0:

                                break;
                            case 1:
                                userCancel();
                                break;
                        }
                    }
                }, true);

                break;
        }
    }

    private void userCancel() {
        JPushInterface.setAliasAndTags(this, "", new LinkedHashSet<String>(), new TagAliasCallback() {
            @Override
            public void gotResult(int i, String s, Set<String> set) {

            }
        });
//        SharedXmlUtil.getInstance().delete(SharedPreferencesKeyConst.LOGIN_USER_NAME);
        SharedXmlUtil.getInstance().delete(SharedPreferencesKeyConst.LOGIN_USER_PASSWORD);
        openActivity(LoginActivity.class);
        finish();
        MainActivity.mainActivityInstance.finish();
    }

}
